package com.ymatou.iapi.trading.sellerquery2.parameter;

import java.util.ArrayList;
import java.util.List;

import com.ymatou.query.seller.trading.api.parameter.GetSellerOrdersListSummaryArguments;
import com.ymatou.query.seller.trading.api.parameter.TimeRange;
import com.ymttest.common.enums.DateTypeEnum;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.DataUtil.KeyValuePair;
import com.ymttest.utils.YMTDateUtil;

public class GetSellerOrdersListSummary {

	public static GetSellerOrdersListSummaryArguments GetArguments(
			GetSellerOrdersListSummaryV2Bean getSellerOrdersListSummaryV2Bean) {
		GetSellerOrdersListSummaryArguments args = new GetSellerOrdersListSummaryArguments();
		args.setSellerId(getSellerOrdersListSummaryV2Bean.getSellerUserId());
		args.setOrderSellerAcceptedStatus(OrderStatusEnum.SellerAccept
				.getValue());

		if (getSellerOrdersListSummaryV2Bean.getFilter() != null) {
			args.setFilter(new KeyValuePair<Integer, String>(
					getSellerOrdersListSummaryV2Bean.getFilter().getKey(),
					getSellerOrdersListSummaryV2Bean.getFilter().getValue()));
		}

		SetPaidInFullAndStatus(getSellerOrdersListSummaryV2Bean, args);
		SetTimeRange(getSellerOrdersListSummaryV2Bean, args);

		return args;
	}

	private static void SetPaidInFullAndStatus(
			GetSellerOrdersListSummaryV2Bean getSellerOrdersListSummaryV2Bean,
			GetSellerOrdersListSummaryArguments args) {
		args.setOrderStatus(Common.GetSellerOrderStatus(
				getSellerOrdersListSummaryV2Bean.getOrderStatusList(), true));
		args.setPaidInFull(Common.paidInFull);
	}

	private static void SetTimeRange(
			GetSellerOrdersListSummaryV2Bean getSellerOrdersListSummaryV2Bean,
			GetSellerOrdersListSummaryArguments args) {
		if (DataUtil.isNullOrEmpty(getSellerOrdersListSummaryV2Bean
				.getDateList()))
			return;
		List<Integer> temp = new ArrayList<Integer>();
		temp.addAll(getSellerOrdersListSummaryV2Bean.getDateList());

		if (temp.removeIf(x -> x == DateTypeEnum.All.getValue())
				&& DataUtil.isNullOrEmpty(temp)) {
			return;
		}

		String today = YMTDateUtil.addDays(0, YMTDateUtil.YMD);

		boolean yesterday = false;
		if (temp.removeIf(x -> x == DateTypeEnum.Yesterday.getValue())) {
			yesterday = true;
			if (DataUtil.isNullOrEmpty(temp)) {
				args.setTimeFrom(YMTDateUtil.addDays(-1, YMTDateUtil.YMD));
				args.setTimeTo(today);
				return;
			}
		}

		TimeRange min = Common.ToTimeRange(temp.stream().min((a, b) -> a - b)
				.get());
		if (null == min) {
			args.setTimeFrom(today);
			args.setTimeTo(YMTDateUtil.getBeforeOrNextDay(today, -1,
					YMTDateUtil.YMD));
			return;
		}

		args.setTimeFrom(YMTDateUtil.getBeforeOrNextDay(min.getBegin(), 0,
				YMTDateUtil.YMD));
		args.setTimeTo(YMTDateUtil.getBeforeOrNextDay(min.getEnd(), 0,
				YMTDateUtil.YMD));

		if (!yesterday) {
			return;
		}

		if (YMTDateUtil.parseDate(args.getTimeFrom(), YMTDateUtil.YMD)
				.compareTo(YMTDateUtil.parseDate(today, YMTDateUtil.YMD)) != 1) {
			args.setTimeFrom(YMTDateUtil.getBeforeOrNextDay(today, -1,
					YMTDateUtil.YMD));
			args.setTimeTo(today);
		} else {
			args.setTimeFrom(today);
			args.setTimeTo(YMTDateUtil.getBeforeOrNextDay(today, -1,
					YMTDateUtil.YMD));
		}
	}
}
